package com.kaili.db.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.common.OperationType;
import com.kaili.common.exception.BaseExcept;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.db.mapper.HsOptEwbMapper;
import com.kaili.db.mapper.HsOptRewbMapper;
import com.kaili.db.service.IHsOptEwbService;
import com.kaili.db.service.IHsOptQueryService;
import com.kaili.db.service.IHsOptRewbService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.rewb.RewbAddDTO;
import com.kaili.pojo.entity.HsOptRewb;
import com.kaili.pojo.entity.RewbBean;
import com.kaili.pojo.vo.RewbQueryVo;
import com.kaili.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.kaili.except.ResponseStatus.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cgm
 * @since 2018-05-08
 */
@Service
@Transactional
public class HsOptRewbServiceImpl extends ServiceImpl<HsOptRewbMapper, HsOptRewb> implements IHsOptRewbService {

    @Autowired
    IHsOptEwbService ewbService;
    @Autowired
    IHsOptQueryService queryService;
    @Autowired
    HsOptEwbMapper hsOptEwbMapper;

    /**
     * 查询条件 type 为
     * 1:回单寄送 未寄
     * 2:回单寄送 已寄
     * 3:回单验收 未验
     * 4:回单验收 已验
     * 5:回单返厂 未返
     * 6:回单返厂 已返
     *
     * @param search
     * @return
     */
    @Override
    public List<RewbQueryVo> getRewbList(Map search) {
        return baseMapper.getRewbList(search);
    }

    @Override
    public Page<RewbQueryVo> getRewbList(Page<RewbQueryVo> page, Map search) {
        return page.setRecords(baseMapper.getRewbList(page, search));
    }

    boolean checkStatus(int type, HsOptRewb rewb) {
        Map search = new HashMap();
        search.put("ewb_no", rewb.getEwbNo());
        search.put("rewb_no", rewb.getRewbNo());
        search.put("rewb_status", type);
        List<HsOptRewb> lst = baseMapper.selectByMap(search);
        if (lst != null && lst.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    boolean checkStatusPda(int type, HsOptRewb rewb) {
        Map search = new HashMap();
        search.put("ewb_no", rewb.getEwbNo());
        if (type == 1) {
            List<HsOptRewb> lst = baseMapper.selectByMap(search);
            if (lst != null && lst.size() > 0) {
                return true;
            } else {
                return false;
            }
        } else if (type == 2) {
            search.put("ewb_no", rewb.getEwbNo());
            List<HsOptRewb> lst = baseMapper.selectByMap(search);
            for (HsOptRewb mlist : lst) {
                if (mlist.getRewbStatus() == 2 || mlist.getRewbStatus() == 3) {
                    return true;
                } else {
                    return false;
                }
            }
        } else if (type == 3) {
            search.put("ewb_no", rewb.getEwbNo());
            List<HsOptRewb> lst = baseMapper.selectByMap(search);
            for (HsOptRewb mlist : lst) {
                if (mlist.getRewbStatus() == 3) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    @Override
    public int doUpdateSendConfirm(List<RewbAddDTO> beans) {

        for (int i = 0; i < beans.size(); i++) {
            RewbAddDTO bean = beans.get(i);
            if (bean.getRewbNo() == null) {
                throw new BaseExcept(REWB_SEND_ERR);
            }
            /*if(!ewbService.isSign(bean.getEwbNo())){
                ewbService.doSignByEwb(bean.getEwbNo());

            }*/
            RewbBean rewbBean = new RewbBean();
            BeanUtils.copyProperties(bean, rewbBean);

            HsOptRewb hsOptRewb = new HsOptRewb();
            hsOptRewb.setCreatedBy(rewbBean.getOptId());
            hsOptRewb.setCreatedTime(DateUtils.getCurrentDate());
            hsOptRewb.setEwbNo(rewbBean.getEwbNo());
            hsOptRewb.setRewbNo(bean.getRewbNo());
            hsOptRewb.setSendConfirmTime(DateUtils.getCurrentDate());
            hsOptRewb.setExpressId(rewbBean.getExpressId());
            hsOptRewb.setRewbStatus(OperationType.REWB_SEND);
            hsOptRewb.setSourceType(1);
            if (checkStatus(OperationType.REWB_SEND, hsOptRewb)) {
                throw new BaseExcept(REWB_SEND_ALREADY_ERR);
            }
            if (baseMapper.insert(hsOptRewb) < 0) {
                throw new BaseExcept(REWB_SEND_ERR);
            }
        }
        return 0;
    }

    @Override
    public int doUpdateCheckConfirm(List<RewbAddDTO> beans) {
        for (int i = 0; i < beans.size(); i++) {
            RewbAddDTO bean = beans.get(i);
            RewbBean rewbBean = new RewbBean();
            BeanUtils.copyProperties(bean, rewbBean);
            HsOptRewb hsOptRewb = new HsOptRewb();
            hsOptRewb.setCheckedBy(rewbBean.getOptId());
            hsOptRewb.setCheckConfirmTime(DateUtils.getCurrentDate());
            hsOptRewb.setRewbStatus(OperationType.REWB_CHECK);
            hsOptRewb.setRewbNo(rewbBean.getRewbNo());
            hsOptRewb.setEwbNo(rewbBean.getEwbNo());
            hsOptRewb.setSourceType(1);
            if (checkStatus(OperationType.REWB_CHECK, hsOptRewb)) {
                throw new BaseExcept(REWB_CHECK_ALREADY_ERR);
            }
            Wrapper<HsOptRewb> wrapper = new EntityWrapper<>();
            wrapper.where("ewb_no={0} and rewb_no={1}", rewbBean.getEwbNo(), rewbBean.getRewbNo());
            if (baseMapper.update(hsOptRewb, wrapper) < 0) {
                throw new BaseExcept(REWB_CHECK_ERR);
            }
        }
        return 0;
    }

    @Override
    public int doUpdateReturnConfirm(List<RewbAddDTO> beans) {
        for (int i = 0; i < beans.size(); i++) {
            RewbAddDTO bean = beans.get(i);
            RewbBean rewbBean = new RewbBean();
            BeanUtils.copyProperties(bean, rewbBean);
            HsOptRewb hsOptRewb = new HsOptRewb();
            hsOptRewb.setReturnedBy(rewbBean.getOptId());
            hsOptRewb.setReturnConfirmTime(DateUtils.getCurrentDate());
            hsOptRewb.setRewbStatus(OperationType.REWB_RETURN);
            hsOptRewb.setEwbNo(rewbBean.getEwbNo());
            hsOptRewb.setRewbNo(rewbBean.getRewbNo());
            hsOptRewb.setSourceType(1);
            if (checkStatus(OperationType.REWB_RETURN, hsOptRewb)) {
                throw new BaseExcept(REWB_RETURN_ALREADY_ERR);
            }
            Wrapper<HsOptRewb> wrapper = new EntityWrapper<>();
            wrapper.where("ewb_no={0} and rewb_no={1}", rewbBean.getEwbNo(), rewbBean.getRewbNo());
            if (baseMapper.update(hsOptRewb, wrapper) < 0) {
                throw new BaseExcept(REWB_RETURN_ERR);
            }
        }
        return 0;
    }

    @Override
    public ResponseStatus doUpdateSendConfirmCS(RewbAddDTO beans) {
        if (beans.getRewbNo() == null) {
            return ResponseStatus.REWB_SEND_ERR;
        }
        RewbBean rewbBean = new RewbBean();
        BeanUtils.copyProperties(beans, rewbBean);
        //判断寄送网点是否为签收网点
        rewbBean.setSiteId(LoginDetailUtils.getSiteId());
        Integer count = hsOptEwbMapper.getSenIsSignSite(rewbBean);
        if (count == 0) {
            return ResponseStatus.REWB_IS_SIGN;
        }

        HsOptRewb hsOptRewb = new HsOptRewb();
        hsOptRewb.setCreatedBy(rewbBean.getOptId());
        hsOptRewb.setCreatedTime(DateUtils.getCurrentDate());
        hsOptRewb.setEwbNo(rewbBean.getEwbNo());
        hsOptRewb.setRewbNo(rewbBean.getRewbNo());
        hsOptRewb.setSendConfirmTime(DateUtils.getCurrentDate());
        hsOptRewb.setExpressId(rewbBean.getExpressId());
        hsOptRewb.setRewbStatus(OperationType.REWB_SEND);
        hsOptRewb.setSourceType(2);
        if (checkStatusPda(OperationType.REWB_SEND, hsOptRewb)) {
            return ResponseStatus.REWB_SEND_ALREADY_ERR;
        }
        if (baseMapper.insert(hsOptRewb) < 0) {
            return ResponseStatus.REWB_SEND_ERR;
        }
        return ResponseStatus.RESPONSE_OK;
    }

    @Override
    public ResponseStatus doUpdateCheckConfirmCS(RewbAddDTO beans) {
        //判断验收网点是否为寄件网点
        RewbBean rewbBean = new RewbBean();
        BeanUtils.copyProperties(beans, rewbBean);
        rewbBean.setSiteId(LoginDetailUtils.getSiteId());
        Integer count = hsOptEwbMapper.acceptancceNetwork(rewbBean);
        if (count == 0) {
            return ResponseStatus.REWB_ACCEPTANCE_RETURN;
        }
        HsOptRewb hsOptRewb = new HsOptRewb();
        hsOptRewb.setCheckedBy(rewbBean.getOptId());
        hsOptRewb.setCheckConfirmTime(DateUtils.getCurrentDate());
        hsOptRewb.setRewbStatus(OperationType.REWB_CHECK);
        hsOptRewb.setRewbNo(rewbBean.getRewbNo());
        hsOptRewb.setEwbNo(rewbBean.getEwbNo());
        hsOptRewb.setSourceType(2);
        if (checkStatusPda(OperationType.REWB_CHECK, hsOptRewb)) {
            return ResponseStatus.REWB_CHECK_ALREADY_ERR;
        }
        Wrapper<HsOptRewb> wrapper = new EntityWrapper<>();
        wrapper.where("ewb_no={0} and rewb_no={1}", rewbBean.getEwbNo(), rewbBean.getRewbNo());
        if (baseMapper.update(hsOptRewb, wrapper) < 0) {
            return ResponseStatus.REWB_CHECK_ERR;
        }
        return ResponseStatus.RESPONSE_OK;
    }

    @Override
    public ResponseStatus doUpdateReturnConfirmCS(RewbAddDTO beans) {
        //判断返厂网点是否寄件网点
        RewbBean rewbBean = new RewbBean();
        BeanUtils.copyProperties(beans, rewbBean);
        rewbBean.setSiteId(LoginDetailUtils.getSiteId());
        Integer count = hsOptEwbMapper.acceptancceNetwork(rewbBean);
        if (count == 0) {
            return ResponseStatus.REWB_ACCEPTANCE_RETURN;
        }
        HsOptRewb hsOptRewb = new HsOptRewb();
        hsOptRewb.setReturnedBy(rewbBean.getOptId());
        hsOptRewb.setReturnConfirmTime(DateUtils.getCurrentDate());
        hsOptRewb.setRewbStatus(OperationType.REWB_RETURN);
        hsOptRewb.setEwbNo(rewbBean.getEwbNo());
        hsOptRewb.setRewbNo(rewbBean.getRewbNo());
        hsOptRewb.setSourceType(2);
        if (checkStatusPda(OperationType.REWB_RETURN, hsOptRewb)) {
            return ResponseStatus.REWB_RETURN_ALREADY_ERR;
        }
        Wrapper<HsOptRewb> wrapper = new EntityWrapper<>();
        wrapper.where("ewb_no={0} and rewb_no={1}", rewbBean.getEwbNo(), rewbBean.getRewbNo());
        if (baseMapper.update(hsOptRewb, wrapper) < 0) {
            return ResponseStatus.REWB_RETURN_ERR;
        }


        return ResponseStatus.RESPONSE_OK;
    }

    @Override
    public List<Map> rewbsCheckEwbNo(HashMap map) {
        return baseMapper.rewbsCheckEwbNo(map);
    }
}
